/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is Indigo framework applicatif.
  
  The Initial Developer of the Original Code is
  Olivier Bugalotto (aka Iteratif) <olivier.bugalotto@iteratif.net>.
  Portions created by the Initial Developer are Copyright (C) 2004-2008
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/package indigo.collections
{
	import flash.errors.IllegalOperationError;
	
	/**
	 *  Represente une collection d'objets premier entre, premier sorti.
	 * 
	 */
	public class Queue implements ICollection
	{
		private var _items:Array;
		
		/**
		 * 
		 * @return 
		 * @inheritDoc
		 */
		public function get count():uint
		{
			return _items.length;
		}
		
		/**
		 * Creer un nouvel objet <code>Queue</code> 
		 * 
		 */
		public function Queue(source:Array = null)
		{
			_items = (source != null) ? source : new Array();
		}

		/**
		 * Ajoute un objet a la fin de Queue.  
		 * @param item
		 * 
		 */
		public function enqueue(item:Object):void {
			_items.push(item);
		}
		
		/**
		 * Supprime et retourne l'objet au debut de Queue.  
		 * @return 
		 * 
		 */
		public function dequeue():Object {
			if(count == 0) throw new IllegalOperationError();
			
			return _items.shift();
		}
		
		/**
		 * Retourne l'objet situe au debut de Queue sans le supprimer.  
		 * @return 
		 * 
		 */
		public function peek():Object {
			if(count == 0) throw new IllegalOperationError();
			
			return _items[0];
		}

		/**
		 * 
		 * @return 
		 * @inheritDoc
		 */
		public function getEnumerator():IEnumerator
		{
			return new Enumerator(_items);
		}
		
		/**
		 *
		 * @inheritDoc
		 */
		public function clear():void {
			_items = [];
		}
		
		/**
		 *
		 * @param item
		 * @return 
		 * @inheritDoc
		 */
		public function contains(item:Object):Boolean {
			var e:IEnumerator = getEnumerator();
			while(e.moveNext()) {
				if(e.current == item)
					return true;
			}
			return false;
		}
		
		/**
		 * 
		 * @return 
		 * @inheritDoc
		 */
		public function arrayTo(index:int = 0):Array
		{
			return _items.splice(index);
		}
		
	}
}